package pannelli.sub;

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;

import database.DB;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import oggetti.NonEditableDefaultTableModel;

import oggetti.Riparazione;
import oggetti.Utente;
import pannelli.VisualizzaUtente;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class SubPannelloVisualizzaUtenti extends javax.swing.JPanel {

    private JTable jTable1;
    private JScrollPane jScrollPane1;
    private DefaultTableModel model;
    private boolean flag = false;

    public SubPannelloVisualizzaUtenti() {
        super();
        initGUI();
        initTable();
    }

    public void initTable() {
        int a = model.getRowCount();
        for (int i = 0; i < a; i++) {
            model.removeRow(0);
        }

        ArrayList<Utente> c = new DB().getUtenti();
        for (Utente tmp : c) {
            model.addRow(tmp.toArray());
        }
    }

    private Utente getUtenteFromRow(int row) {
        String username = (String) jTable1.getValueAt(row, 0);
        Utente u = new DB().getUtenteFromUsername(username);
        return u;
    }

    private void initGUI() {
        try {
            AnchorLayout thisLayout = new AnchorLayout();
            this.setLayout(thisLayout);
            this.setPreferredSize(new java.awt.Dimension(486, 421));
            {
                jScrollPane1 = new JScrollPane();
                this.add(jScrollPane1, new AnchorConstraint(1, 1001, 1001, 1, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
                jScrollPane1.setPreferredSize(new java.awt.Dimension(486, 421));
                {
                    model = new NonEditableDefaultTableModel();
                    jTable1 = new JTable(model);
                    jScrollPane1.setViewportView(jTable1);
                    String[] columnNames = {"Username",
                        "Password", "Nome", "Cognome", "E-mail"};
                    for (int i = 0; i < columnNames.length; i++) {
                        model.addColumn(columnNames[i]);
                    }

                    jTable1.addMouseListener(new MouseAdapter() {

                        @Override
                        public void mouseClicked(MouseEvent arg0) {
                            if (arg0.getClickCount() >= 2) {
                                if (!flag) {
                                    int row = jTable1.getSelectedRow();
                                    if (row >= 0) {
                                        new VisualizzaUtente(getUtenteFromRow(row)).setVisible(true);
                                    }
                                }
                            }
                        }
                    });
                    jTable1.setPreferredSize(new java.awt.Dimension(468, 399));

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setFlag(boolean b) {
        this.flag = b;

    }
}
